//
//  ViewController.m
//  05 -- QQ 好友列表展示
//
//  Created by Rainy Zou on 15/11/25.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "ViewController.h"
#import "FriendGroup.h"
#import "Friend.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *groupArray;

@end

@implementation ViewController


- (NSArray *)groupArray
{
    if(_groupArray == nil){
        _groupArray = [FriendGroup friendGroupList];
    }
    return _groupArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
}


#pragma mark 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    FriendGroup *group = self.groupArray[section];
    
    return group.isShow? group.friends.count:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuseId = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
    }
    
    FriendGroup *group = self.groupArray[indexPath.section];
    Friend *friend = group.friends[indexPath.row];
    
//    UIImage *image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:friend.icon ofType:nil]];
    [cell.imageView setImage:[UIImage imageNamed:friend.icon]];
    
    cell.textLabel.text = friend.name;
    cell.detailTextLabel.text = friend.intro;
    
    if (friend.isVip) {
        cell.textLabel.textColor = [UIColor redColor];
    }else{
        cell.textLabel.textColor = [UIColor blackColor];
    }
    
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.groupArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    FriendGroup *group = self.groupArray[section];
    return  group.name;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    FriendGroup *group = self.groupArray[section];
    
    static NSString *reuseId = @"headView";
    
    UITableViewHeaderFooterView *view = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (view == nil) {
        view = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:reuseId];
        view.frame = CGRectMake(0, 0, 375, 44);
        
        UIButton *btn = [[UIButton alloc]init];
        btn.frame = view.bounds;
        [view addSubview:btn];
        
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg.png"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted.png"] forState:UIControlStateHighlighted];
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow.png"] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [btn setContentEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        
        //图片大于imageView,被缩放到UIImageView的大小
        btn.imageView.contentMode = UIViewContentModeScaleToFill;
        //设置超出部分不裁剪
        btn.imageView.clipsToBounds = NO;
        //设置图片不缩放
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(200, 0, 170, 44);
        [view addSubview:label];
        label.textAlignment = NSTextAlignmentRight;
    }
    
    for(UIView *subView in view.subviews)
    {
        if([subView isKindOfClass:[UIButton class]]){
            UIButton *button = subView;
            button.tag = section+10;
            [button setTitle:group.name forState:UIControlStateNormal] ;
        }else if([subView isKindOfClass:[UILabel class]])
        {
            UILabel *label = subView;
            label.text = [NSString stringWithFormat:@"%d/%d",group.online,group.friends.count];
        }
    }
    
    
    return view;
}


- (void)btnClick:(UIButton *)button
{
    NSInteger clickSection = button.tag -10;
    FriendGroup *group = self.groupArray[clickSection];
    if(group.isShow){
        button.imageView.transform = CGAffineTransformMakeRotation(0);
    }else{
        
        button.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
    group.show = !group.show;
    
    [self.tableView reloadData];
    
}

@end
